import PaletteProvider from 'bpmn-js/lib/features/palette/PaletteProvider'
import { assign } from 'min-dash'
import { createAction } from './utils'

type Palette = import('diagram-js/lib/features/palette/Palette').default
type Create = import('diagram-js/lib/features/create/Create').default
type ElementFactory = import('diagram-js/lib/core/ElementFactory').default
type SpaceTool = import('bpmn-js/lib/features/space-tool/BpmnSpaceTool').default
type LassoTool = import('diagram-js/lib/features/lasso-tool/LassoTool').default
type HandTool = import('diagram-js/lib/features/hand-tool/HandTool').default
type GlobalConnect = import('diagram-js/lib/features/global-connect/GlobalConnect').default
export type Translate = typeof import('diagram-js/lib/i18n/translate/translate').default

class RewritePaletteProvider extends PaletteProvider {
  private readonly _palette: Palette
  private readonly _create: Create
  private readonly _elementFactory: ElementFactory
  private readonly _spaceTool: SpaceTool
  private readonly _lassoTool: LassoTool
  private readonly _handTool: HandTool
  private readonly _globalConnect: GlobalConnect
  private readonly _translate: Translate
  constructor(
    palette: Palette,
    create: Create,
    elementFactory: ElementFactory,
    spaceTool: SpaceTool,
    lassoTool: LassoTool,
    handTool: HandTool,
    globalConnect: GlobalConnect,
    translate: Translate,
  ) {
    super(palette, create, elementFactory, spaceTool, lassoTool, handTool, globalConnect, translate)
    this._palette = palette
    this._create = create
    this._elementFactory = elementFactory
    this._spaceTool = spaceTool
    this._lassoTool = lassoTool
    this._handTool = handTool
    this._globalConnect = globalConnect
    this._translate = translate
  }
  getPaletteEntries() {
    const actions = {},
      create = this._create,
      elementFactory = this._elementFactory,
      lassoTool = this._lassoTool,
      handTool = this._handTool,
      globalConnect = this._globalConnect

    function createSubprocess(event: any) {
      const subProcess = elementFactory.createShape({
        type: 'bpmn:SubProcess',
        x: 0,
        y: 0,
        isExpanded: true,
      })

      const startEvent = elementFactory.createShape({
        type: 'bpmn:StartEvent',
        x: 40,
        y: 82,
        parent: subProcess,
      })

      create.start(event, [subProcess, startEvent], {
        hints: {
          autoSelect: [startEvent],
        },
      })
    }

    assign(actions, {
      'hand-tool': {
        group: 'tools',
        className: 'bpmn-icon-hand-tool',
        title: '手型工具',
        action: {
          click: function (event) {
            handTool.activateHand(event)
          },
        },
      },
      'lasso-tool': {
        group: 'tools',
        className: 'bpmn-icon-lasso-tool',
        title: '套索工具',
        action: {
          click: function (event: MouseEvent) {
            lassoTool.activateSelection(event)
          },
        },
      },
      'global-connect-tool': {
        group: 'tools',
        className: 'bpmn-icon-connection-multi',
        title: '全局连线',
        action: {
          click: function (event) {
            globalConnect.toggle(event)
          },
        },
      },
      'tool-separator': {
        group: 'tools',
        separator: true,
      },
      'create.start-event': createAction(
        elementFactory,
        create,
        'bpmn:StartEvent',
        'events',
        'bpmn-icon-start-event-none',
        '开始事件',
      ),
      'create.end-event': createAction(
        elementFactory,
        create,
        'bpmn:EndEvent',
        'events',
        'bpmn-icon-end-event-none',
        '结束事件',
      ),
      'events-separator': {
        group: 'events',
        separator: true,
      },
      'create.exclusive-gateway': createAction(
        elementFactory,
        create,
        'bpmn:ExclusiveGateway',
        'gateway',
        'bpmn-icon-gateway-xor',
        '网关',
      ),
      'create.parallel-gateway': createAction(
        elementFactory,
        create,
        'bpmn:ParallelGateway',
        'gateway',
        'bpmn-icon-gateway-parallel',
        '并行网关',
      ),
      // 'create.event-base-gateway': createAction(
      //   elementFactory,
      //   create,
      //   'bpmn:EventBasedGateway',
      //   'gateway',
      //   'bpmn-icon-gateway-eventbased',
      //   '事件网关',
      // ),
      'gateway-separator': {
        group: 'gateway',
        separator: true,
      },
      'create.user-task': createAction(
        elementFactory,
        create,
        'bpmn:UserTask',
        'activity',
        'bpmn-icon-user-task',
        '用户任务',
      ),
      // 'create.script-task': createAction(
      //   elementFactory,
      //   create,
      //   'bpmn:ScriptTask',
      //   'activity',
      //   'bpmn-icon-script-task',
      //   '脚本任务',
      // ),
      // 'create.service-task': createAction(
      //   elementFactory,
      //   create,
      //   'bpmn:ServiceTask',
      //   'activity',
      //   'bpmn-icon-service-task',
      //   '服务任务',
      // ),
      'create.subprocess-expanded': {
        group: 'activity',
        className: 'bpmn-icon-subprocess-expanded',
        title: '子流程',
        action: {
          dragstart: createSubprocess,
          click: createSubprocess,
        },
      },
    })

    return actions
  }
}

RewritePaletteProvider.$inject = [
  'palette',
  'create',
  'elementFactory',
  'spaceTool',
  'lassoTool',
  'handTool',
  'globalConnect',
]

export default RewritePaletteProvider
